package com.kfm.web.controller.product;

import com.kfm.common.domain.entity.Product;
import com.kfm.common.domain.response.Result;
import com.kfm.product.service.ProductImagesService;
import com.kfm.product.service.ProductService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductInfoController {

    @Resource
    private ProductService productService;

    @Resource
    private ProductImagesService productImagesService;

    @RequestMapping("/list")
    public Result list(Product product) {
        List<Product> list = productService.selectByCondition(product);
        return list.isEmpty() ? Result.failure("查询失败") : Result.success("查询成功", list);
    }

    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id")Integer id) {
        Product product = productService.selectByPrimaryKey(id);
        if (product == null) {
            return Result.failure("查询失败");
        }
        product.setImagesList(productImagesService.findByProductId(id));
        return Result.success("查询成功", product);
    }

    // /**
    //  * 添加了映射之后使用这个接口
    //  *
    //  * @param id 商品id
    //  * @return 商品详情
    //  */
    // @RequestMapping("/info/{id}")
    // public Result info(@PathVariable("id") Integer id) {
    //     Product product = productService.selectByPrimaryKey(id);
    //     return product == null ? Result.failure("查询失败") : Result.success("查询成功", product);
    // }
}
